﻿using System;
using System.Collections.Generic;
using gbL.NetBoard.Helpers;
using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;

namespace gbL.NetBoard.Examples.DoubleMVC.View.UI
{
    public class MenuView : TypedMessageListener<MenuViewRequest>
    {
        public IOutputTarget Output { get; set; }

        public MenuView(Blackboard domain) : this(domain, new ConsoleOutputTarget()) { }

        public MenuView(Blackboard domain, IOutputTarget output)
            : base(domain)
        {
            Output = output;
        }

        protected override void OnMessageAdded(MenuViewRequest message)
        {
            // clear this message from the board
            IList<IBlackboardEntry> entries = Messages.Take(e => e.Message == message);
            if (entries.Count > 0) Process(message);
        }

        private void Process(MenuViewRequest message)
        {
            Output.Clear();
            Output.WriteLine(message.MenuTitle);
            Output.WriteLine(new string('-', message.MenuTitle.Length));
            Output.WriteLine(message.Content);
        }

        protected override void OnMessageRemoved(MenuViewRequest message)
        {
            // not needed
        }
    }
}
